package com.lsyw.intergrate.common;

import lombok.Data;

/**
 * 统一返回
 */
@Data
public class BaseResult<T> {
    public static int SUCCESS = ErrorCode.C0.getErrcode();
    public static int ERROR = ErrorCode.C1.getErrcode();

    /**
     * 代码 1 成功 0 失败
     */
    int code;

    /**
     * 数据
     */
    T data;

    /**
     * 消息
     */
    String message;

    public BaseResult(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 接口增加返回值
     *
     * @param insert 状态
     * @param data   返回数据
     * @return
     */
    public static <T>BaseResult<T> addResult(boolean insert, T data) {
        if (insert) {
            return new BaseResult<T>(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new BaseResult<T>(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }


    /**
     * 接口删除返回值
     *
     * @param delete 状态
     * @param data   返回数据
     * @return
     */
    public static <T> BaseResult<T> deleteResult(Integer delete, T data) {
        if (delete > 0) {
            return new BaseResult<T>(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new BaseResult<T>(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }

    /**
     * 接口修改返回值
     *
     * @param update 状态
     * @param data   返回数据
     * @return
     */
    public static <T> BaseResult<T> updateResult(boolean update, T data) {
        if (update) {
            return new BaseResult<T>(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new BaseResult<T>(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }

    /**
     * 接口查询返回值
     *
     * @param data 返回数据
     * @return
     */
    public static <T> BaseResult<T> searchResult(T data) {
        if (data != null) {
            return new BaseResult<T>(SUCCESS, data, ErrorCode.C0.getErrmsg());
        } else {
            return new BaseResult<T>(ERROR, data, ErrorCode.C1.getErrmsg());
        }
    }


    /**
     * 成功返回
     *
     * @param data 返回数据
     * @return
     */
    public static <T> BaseResult<T> successResult(T data) {
        return new BaseResult<T>(SUCCESS, data, ErrorCode.C0.getErrmsg());

    }

    /**
     * 失败返回
     *
     * @param data 返回数据
     * @return
     */
    public static <T> BaseResult<T> errorResult(T data) {
        return new BaseResult<T>(ERROR, data, data.toString());

    }


    /**
     * 失败返回
     *
     * @param data 返回数据
     * @return
     */
    public static  BaseResult errorResult(Object data, String message) {
        return new BaseResult(ERROR, data, message);
    }

}
